<?php

class Test_Tbszip {    

	private $_zipFile = './tmp.zip';
	private $_file = './test.txt';

	public function __construct() {
        V::addLibrary('third', dirname(V_PATH).'/third-lib/');
        
		$this->_zipFile = dirname(__FILE__).'/tmp.zip';
		$this->_file = dirname(__FILE__).'/test.txt';
	}

    public function testCreate() {

		$zip = new TbsZip();
		$zip->CreateNew();
		file_put_contents($this->_file, 'test');
		$zip->FileAdd(basename($this->_file), $this->_file, TBSZIP_FILE);
		$zip->FileAdd('string.txt', 'this is a string text', TBSZIP_STRING);
		$zip->Flush(TBSZIP_FILE, $this->_zipFile);
		$zip->close();
		unlink($this->_file);
        return file_exists($this->_zipFile) ? true : false;
    }

    public function testRead() {

		$zip = new TbsZip();
		$zip->Open($this->_zipFile);
		echo count($zip->CdFileLst), ' files found in zip file', PHP_EOL;

		$ok = $zip->FileExists(basename($this->_file));
		if ($ok) {
			echo basename($this->_file), ':', PHP_EOL;
			echo $zip->FileRead(basename($this->_file)), PHP_EOL;
		} else {
			echo 'warning: ', basename($this->_file), ' does not exist!', PHP_EOL;
		}

		$ok = $zip->FileExists('string.txt');
		if ($ok) {
			echo 'string.txt', ':', PHP_EOL;
			echo $zip->FileRead('string.txt'), PHP_EOL;
		} else {
			echo 'warning: ', 'string.txt', ' does not exist!', PHP_EOL;
		}
		$zip->close();
		unlink($this->_zipFile);
        return true;
    }
}